package com.xiaozhen.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.common.core.domain.driver.ParseDriver;
import com.xiaozhen.common.core.domain.driver.ParseTcpDriver;
import com.xiaozhen.driver.dao.ParseTcpDriverDao;
import com.xiaozhen.driver.service.ParseTcpDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/29 10:09
 * @description:
 */
@Service
public class ParseTcpDriverServiceImpl implements ParseTcpDriverService {

    @Autowired
    private ParseTcpDriverDao tcpDriverDao;

    @Override
    public List<ParseTcpDriver> selectDriverList(ParseDriver parseDriver) {
        QueryWrapper<ParseTcpDriver> queryWrapper = new QueryWrapper<>();
        if (parseDriver.getDriverId() != null) {
            queryWrapper.eq("driver_id", parseDriver.getDriverId());
        }
        return tcpDriverDao.selectList(queryWrapper);
    }


    @Override
    public Boolean checkDriverPortUnique(ParseTcpDriver tcpDriver) {
        QueryWrapper<ParseTcpDriver> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("port", tcpDriver.getPort());
        List<ParseTcpDriver> tcpDrivers = tcpDriverDao.selectList(queryWrapper);
        if (tcpDrivers.size() == 0) {
            return true;
        } else if (tcpDriver.getDriverId() != null && tcpDriver.getDriverId().equals(tcpDrivers.get(0).getDriverId())) {
            return true;
        }
        return false;
    }

    @Override
    public Integer insertDriver(ParseTcpDriver tcpDriver) {
        return tcpDriverDao.insert(tcpDriver);
    }

    @Override
    public Integer updateDriver(ParseTcpDriver tcpDriver) {
        return tcpDriverDao.updateById(tcpDriver);
    }

    @Override
    public Integer deleteDriverById(Long driverId) {
        return tcpDriverDao.deleteById(driverId);
    }

    @Override
    public ParseTcpDriver getDriverById(Long driverId) {
        return tcpDriverDao.selectById(driverId);
    }
}
